using System;
using System.Drawing;
using System.Drawing.Drawing2D; // for LinearGradientBrush
using System.Drawing.Text; // for InstalledFontCollection
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Globalization; // for CultureInfo

namespace FB.WinForms.Controls
{
	/// <summary>
	/// Provides a user interface for maintaining the <cref name="HtmlDropDownList.Items"/> collection.
	/// </summary>
	public class HtmlDropDownListItemsEditorUI : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ImageList imageListToolbar;
		private System.Windows.Forms.ToolBar toolBar1;
		private System.Windows.Forms.ToolBarButton toolBarFont;
		private System.Windows.Forms.ToolBarButton toolBarSeparator1;
		private System.Windows.Forms.ToolBarButton toolBarColor;
		private System.Windows.Forms.ToolBarButton toolBarSeparator2;
		private System.Windows.Forms.ToolBarButton toolBarBold;
		private System.Windows.Forms.ToolBarButton toolBarItalic;
		private System.Windows.Forms.ToolBarButton toolBarUnderline;
		private System.Windows.Forms.ToolBarButton toolBarSeparator3;
		private System.Windows.Forms.ToolBarButton toolBarStrikeThough;
		private System.Windows.Forms.ToolBarButton toolBarSeparator4;
		private System.Windows.Forms.ToolBarButton toolBarImage;
		private System.Windows.Forms.ToolBarButton toolBarSeparator0;
		private System.Windows.Forms.ToolBarButton toolBarAnchor;
		private System.Windows.Forms.ToolBarButton toolBarSeparator5;
		private System.Windows.Forms.ToolBarButton toolBarLinearGradientBrush;
		private System.Windows.Forms.ToolBarButton toolBarSeparator6;
		private System.Windows.Forms.ToolBarButton toolBarShadowBrush;
		private System.Windows.Forms.ToolBarButton toolBarSeparator7;
		private FB.WinForms.Controls.HtmlDropDownList comboBoxFonts;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Panel panel2;
		private FB.WinForms.Controls.HtmlDropDownListSimpleStyle htmlDropDownListEdit;
		private System.Windows.Forms.Button buttonAdd;
		private System.Windows.Forms.Button buttonRemove;
		private System.Windows.Forms.Button buttonCancel;
		private System.Windows.Forms.Button buttonOK;
		private System.Windows.Forms.Button buttonUp;
		private System.Windows.Forms.Button buttonDown;
		private System.Windows.Forms.Button buttonSortAscending;

		private System.ComponentModel.IContainer components;

		private float mAngle = 45f;
		private Color mCurrentColor = SystemColors.WindowText;
		private Color mEndColor = Color.Blue;
		private int mOffset = 3;
		private int mSelectedIndex;
		private Color mShadowColor = Color.Blue;
		private Color mStartColor = Color.Red;
		private int mToolBarColorImageIndex;
		private int mToolBarLinearGradientBrushImageIndex;
		private System.Windows.Forms.Button buttonApply;
		private int mToolBarShadowBrushImageIndex;

		/// <summary>
		/// Creates a new <see cref="HtmlDropDownListItemsEditorUI"/> instance.
		/// </summary>
		/// <param name="imageList">Image list.</param>
		/// <param name="baseHref">Base href.</param>
		/// <remarks>
		/// This overload of the Constructor is only used by the <cref name="HtmlDropDownListItemsEditor"/> to
		/// pass the <cref name="this.ImageList"/> or the <cref name="this.BaseRef"/>.
		/// </remarks>
		internal HtmlDropDownListItemsEditorUI(ImageList imageList, string baseHref)
		{
			InitializeComponent();

			htmlDropDownListEdit.ImageList = imageList;
			htmlDropDownListEdit.BaseHref = baseHref;

			mSelectedIndex = htmlDropDownListEdit.SelectedIndex;

			// Create DropDownMenu if the imagelist is populated
			if (htmlDropDownListEdit.ImageList.Images.Count > 0)
			{
				ContextMenu menu = new ContextMenu();
				toolBarImage.Style = System.Windows.Forms.ToolBarButtonStyle.DropDownButton;
				for (int i = 0; i < htmlDropDownListEdit.ImageList.Images.Count; i++)
				{
					MenuItem mi = new MenuItem();
					mi.OwnerDraw = true;
					mi.Click += new System.EventHandler(menuItemImage_Click);
					mi.DrawItem += new DrawItemEventHandler(menuItemImage_DrawItem);
					mi.MeasureItem += new MeasureItemEventHandler(menuItemImage_MeasureItem);
   
					menu.MenuItems.Add(mi);
				}
				toolBarImage.DropDownMenu = menu;
			}

			// Dynamically create ToolbarButton images and remember position in imagelist
			// as we update the images later.
			imageListToolbar.Images.Add(CreateColorBitmapButton(Color.FromKnownColor(KnownColor.WindowText)));
			ToolBarColorImageIndex = imageListToolbar.Images.Count - 1;
			toolBarColor.ImageIndex = ToolBarColorImageIndex;

			imageListToolbar.Images.Add(CreateLinearGradientBrushBitmapButton(StartColor, EndColor, Angle));
			ToolBarLinearGradientBrushImageIndex = imageListToolbar.Images.Count - 1;
			toolBarLinearGradientBrush.ImageIndex = ToolBarLinearGradientBrushImageIndex;

			imageListToolbar.Images.Add(CreateShadowBrushBitmapButton(ShadowColor, Offset));
			ToolBarShadowBrushImageIndex = imageListToolbar.Images.Count - 1;
			toolBarShadowBrush.ImageIndex = ToolBarShadowBrushImageIndex;

			// Set the toolBarFont width to match the font combobox and position over toolBarFont
			NativeMethods.SetButtonSize(toolBar1.Handle, toolBar1.Buttons.IndexOf(toolBarFont), (short)panel1.ClientRectangle.Width); 
			panel1.Parent = toolBar1; 
			panel1.Location = new Point(toolBarFont.Rectangle.X, toolBarFont.Rectangle.Y); 

			// Setup Click event handlers for each ToolBarButton.
			toolBarColor.Tag = new ToolBarButtonClickEventHandler(toolBarColor_Click);
			toolBarBold.Tag = new ToolBarButtonClickEventHandler(toolBarBold_Click);
			toolBarItalic.Tag = new ToolBarButtonClickEventHandler(toolBarItalic_Click);
			toolBarUnderline.Tag = new ToolBarButtonClickEventHandler(toolBarUnderline_Click);
			toolBarStrikeThough.Tag = new ToolBarButtonClickEventHandler(toolBarStrikeThough_Click);
			toolBarImage.Tag = new ToolBarButtonClickEventHandler(toolBarImage_Click);
			toolBarAnchor.Tag = new ToolBarButtonClickEventHandler(toolBarAnchor_Click);
			toolBarLinearGradientBrush.Tag = new ToolBarButtonClickEventHandler(toolBarLinearGradientBrush_Click);
			toolBarShadowBrush.Tag = new ToolBarButtonClickEventHandler(toolBarShadowBrush_Click);

			// Load font names
			comboBoxFonts.BeginUpdate();
			InstalledFontCollection lInstalledFonts = new InstalledFontCollection();
			FontFamily[] lFontFamilies = lInstalledFonts.Families;
			for(int j = 0; j < lFontFamilies.Length; ++j)
			{
				if (lFontFamilies[j].IsStyleAvailable(FontStyle.Regular) == true)
				{
					comboBoxFonts.Items.Add(String.Format(CultureInfo.InvariantCulture, "<font face=\"{0}\">{0}</font>", lFontFamilies[j].Name)); 
					if (j % 5 == 0)
					{
						Application.DoEvents();
					}
				}
			}
			comboBoxFonts.EndUpdate();
			comboBoxFonts.SelectedIndex = comboBoxFonts.FindString(String.Format("<font face=\"{0}\">", this.Font.FontFamily.Name), -1);
			comboBoxFonts.SelectNextControl(comboBoxFonts, true, true, false, false);
			comboBoxFonts.SelectedIndexChanged += new System.EventHandler(this.comboBoxFonts_SelectedIndexChanged);		
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(HtmlDropDownListItemsEditorUI));
			this.toolBar1 = new System.Windows.Forms.ToolBar();
			this.toolBarSeparator0 = new System.Windows.Forms.ToolBarButton();
			this.toolBarColor = new System.Windows.Forms.ToolBarButton();
			this.toolBarSeparator2 = new System.Windows.Forms.ToolBarButton();
			this.toolBarBold = new System.Windows.Forms.ToolBarButton();
			this.toolBarItalic = new System.Windows.Forms.ToolBarButton();
			this.toolBarUnderline = new System.Windows.Forms.ToolBarButton();
			this.toolBarSeparator3 = new System.Windows.Forms.ToolBarButton();
			this.toolBarStrikeThough = new System.Windows.Forms.ToolBarButton();
			this.toolBarSeparator4 = new System.Windows.Forms.ToolBarButton();
			this.toolBarImage = new System.Windows.Forms.ToolBarButton();
			this.toolBarSeparator1 = new System.Windows.Forms.ToolBarButton();
			this.toolBarAnchor = new System.Windows.Forms.ToolBarButton();
			this.toolBarSeparator5 = new System.Windows.Forms.ToolBarButton();
			this.toolBarLinearGradientBrush = new System.Windows.Forms.ToolBarButton();
			this.toolBarSeparator6 = new System.Windows.Forms.ToolBarButton();
			this.toolBarShadowBrush = new System.Windows.Forms.ToolBarButton();
			this.toolBarSeparator7 = new System.Windows.Forms.ToolBarButton();
			this.toolBarFont = new System.Windows.Forms.ToolBarButton();
			this.imageListToolbar = new System.Windows.Forms.ImageList(this.components);
			this.panel1 = new System.Windows.Forms.Panel();
			this.comboBoxFonts = new FB.WinForms.Controls.HtmlDropDownList();
			this.buttonAdd = new System.Windows.Forms.Button();
			this.buttonRemove = new System.Windows.Forms.Button();
			this.buttonCancel = new System.Windows.Forms.Button();
			this.buttonOK = new System.Windows.Forms.Button();
			this.panel2 = new System.Windows.Forms.Panel();
			this.htmlDropDownListEdit = new FB.WinForms.Controls.HtmlDropDownListSimpleStyle();
			this.buttonUp = new System.Windows.Forms.Button();
			this.buttonDown = new System.Windows.Forms.Button();
			this.buttonSortAscending = new System.Windows.Forms.Button();
			this.buttonApply = new System.Windows.Forms.Button();
			this.panel1.SuspendLayout();
			this.panel2.SuspendLayout();
			this.SuspendLayout();
			// 
			// toolBar1
			// 
			this.toolBar1.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
			this.toolBar1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
																						this.toolBarSeparator0,
																						this.toolBarColor,
																						this.toolBarSeparator2,
																						this.toolBarBold,
																						this.toolBarItalic,
																						this.toolBarUnderline,
																						this.toolBarSeparator3,
																						this.toolBarStrikeThough,
																						this.toolBarSeparator4,
																						this.toolBarImage,
																						this.toolBarSeparator1,
																						this.toolBarAnchor,
																						this.toolBarSeparator5,
																						this.toolBarLinearGradientBrush,
																						this.toolBarSeparator6,
																						this.toolBarShadowBrush,
																						this.toolBarSeparator7,
																						this.toolBarFont});
			this.toolBar1.DropDownArrows = true;
			this.toolBar1.ImageList = this.imageListToolbar;
			this.toolBar1.Location = new System.Drawing.Point(0, 0);
			this.toolBar1.Name = "toolBar1";
			this.toolBar1.ShowToolTips = true;
			this.toolBar1.Size = new System.Drawing.Size(584, 30);
			this.toolBar1.TabIndex = 30;
			this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
			// 
			// toolBarSeparator0
			// 
			this.toolBarSeparator0.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// toolBarColor
			// 
			this.toolBarColor.ToolTipText = "Select Color";
			// 
			// toolBarSeparator2
			// 
			this.toolBarSeparator2.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// toolBarBold
			// 
			this.toolBarBold.ImageIndex = 0;
			this.toolBarBold.ToolTipText = "Bold";
			// 
			// toolBarItalic
			// 
			this.toolBarItalic.ImageIndex = 1;
			this.toolBarItalic.ToolTipText = "Italic";
			// 
			// toolBarUnderline
			// 
			this.toolBarUnderline.ImageIndex = 2;
			this.toolBarUnderline.ToolTipText = "Underline";
			// 
			// toolBarSeparator3
			// 
			this.toolBarSeparator3.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// toolBarStrikeThough
			// 
			this.toolBarStrikeThough.ImageIndex = 3;
			this.toolBarStrikeThough.ToolTipText = "Strike Though";
			// 
			// toolBarSeparator4
			// 
			this.toolBarSeparator4.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// toolBarImage
			// 
			this.toolBarImage.ImageIndex = 4;
			this.toolBarImage.ToolTipText = "Select Image";
			// 
			// toolBarSeparator1
			// 
			this.toolBarSeparator1.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// toolBarAnchor
			// 
			this.toolBarAnchor.ImageIndex = 5;
			this.toolBarAnchor.ToolTipText = "Anchor";
			// 
			// toolBarSeparator5
			// 
			this.toolBarSeparator5.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// toolBarLinearGradientBrush
			// 
			this.toolBarLinearGradientBrush.ToolTipText = "Linear Gradient Brush";
			// 
			// toolBarSeparator6
			// 
			this.toolBarSeparator6.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// toolBarShadowBrush
			// 
			this.toolBarShadowBrush.ToolTipText = "Shadow Brush";
			// 
			// toolBarSeparator7
			// 
			this.toolBarSeparator7.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// toolBarFont
			// 
			this.toolBarFont.ToolTipText = "Select Font";
			// 
			// imageListToolbar
			// 
			this.imageListToolbar.ImageSize = new System.Drawing.Size(16, 16);
			this.imageListToolbar.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageListToolbar.ImageStream")));
			this.imageListToolbar.TransparentColor = System.Drawing.Color.Silver;
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.comboBoxFonts);
			this.panel1.Location = new System.Drawing.Point(344, 8);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(232, 24);
			this.panel1.TabIndex = 20;
			// 
			// comboBoxFonts
			// 
			this.comboBoxFonts.Dock = System.Windows.Forms.DockStyle.Top;
			this.comboBoxFonts.ItemHeight = 13;
			this.comboBoxFonts.Location = new System.Drawing.Point(0, 0);
			this.comboBoxFonts.Name = "comboBoxFonts";
			this.comboBoxFonts.Size = new System.Drawing.Size(232, 19);
			this.comboBoxFonts.TabIndex = 2;
			this.comboBoxFonts.TabStop = false;
			// 
			// buttonAdd
			// 
			this.buttonAdd.Location = new System.Drawing.Point(8, 232);
			this.buttonAdd.Name = "buttonAdd";
			this.buttonAdd.TabIndex = 5;
			this.buttonAdd.Text = "Add";
			this.buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click);
			// 
			// buttonRemove
			// 
			this.buttonRemove.Location = new System.Drawing.Point(88, 232);
			this.buttonRemove.Name = "buttonRemove";
			this.buttonRemove.TabIndex = 6;
			this.buttonRemove.Text = "Remove";
			this.buttonRemove.Click += new System.EventHandler(this.buttonRemove_Click);
			// 
			// buttonCancel
			// 
			this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.buttonCancel.Location = new System.Drawing.Point(504, 232);
			this.buttonCancel.Name = "buttonCancel";
			this.buttonCancel.TabIndex = 9;
			this.buttonCancel.Text = "Cancel";
			this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
			// 
			// buttonOK
			// 
			this.buttonOK.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.buttonOK.Location = new System.Drawing.Point(424, 232);
			this.buttonOK.Name = "buttonOK";
			this.buttonOK.TabIndex = 8;
			this.buttonOK.Text = "OK";
			this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
			// 
			// panel2
			// 
			this.panel2.Controls.Add(this.htmlDropDownListEdit);
			this.panel2.Location = new System.Drawing.Point(0, 32);
			this.panel2.Name = "panel2";
			this.panel2.Size = new System.Drawing.Size(544, 192);
			this.panel2.TabIndex = 31;
			// 
			// htmlDropDownListEdit
			// 
			this.htmlDropDownListEdit.Dock = System.Windows.Forms.DockStyle.Top;
			this.htmlDropDownListEdit.Location = new System.Drawing.Point(0, 0);
			this.htmlDropDownListEdit.Name = "htmlDropDownListEdit";
			this.htmlDropDownListEdit.Size = new System.Drawing.Size(544, 192);
			this.htmlDropDownListEdit.TabIndex = 0;
			this.htmlDropDownListEdit.SelectedIndexChanged += new System.EventHandler(this.htmlDropDownListEdit_SelectedIndexChanged);
			// 
			// buttonUp
			// 
			this.buttonUp.ImageIndex = 6;
			this.buttonUp.ImageList = this.imageListToolbar;
			this.buttonUp.Location = new System.Drawing.Point(548, 56);
			this.buttonUp.Name = "buttonUp";
			this.buttonUp.Size = new System.Drawing.Size(32, 31);
			this.buttonUp.TabIndex = 32;
			this.buttonUp.Click += new System.EventHandler(this.buttonUp_Click);
			// 
			// buttonDown
			// 
			this.buttonDown.ImageIndex = 7;
			this.buttonDown.ImageList = this.imageListToolbar;
			this.buttonDown.Location = new System.Drawing.Point(548, 96);
			this.buttonDown.Name = "buttonDown";
			this.buttonDown.Size = new System.Drawing.Size(32, 31);
			this.buttonDown.TabIndex = 33;
			this.buttonDown.Click += new System.EventHandler(this.buttonDown_Click);
			// 
			// buttonSortAscending
			// 
			this.buttonSortAscending.ImageIndex = 8;
			this.buttonSortAscending.ImageList = this.imageListToolbar;
			this.buttonSortAscending.Location = new System.Drawing.Point(548, 136);
			this.buttonSortAscending.Name = "buttonSortAscending";
			this.buttonSortAscending.Size = new System.Drawing.Size(32, 31);
			this.buttonSortAscending.TabIndex = 34;
			this.buttonSortAscending.Click += new System.EventHandler(this.buttonSortAscending_Click);
			// 
			// buttonApply
			// 
			this.buttonApply.Location = new System.Drawing.Point(344, 232);
			this.buttonApply.Name = "buttonApply";
			this.buttonApply.TabIndex = 7;
			this.buttonApply.Text = "Apply";
			this.buttonApply.Click += new System.EventHandler(this.buttonApply_Click);
			// 
			// HtmlDropDownListItemsEditorUI
			// 
			this.AcceptButton = this.buttonOK;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.buttonCancel;
			this.ClientSize = new System.Drawing.Size(584, 262);
			this.Controls.Add(this.buttonApply);
			this.Controls.Add(this.buttonSortAscending);
			this.Controls.Add(this.buttonDown);
			this.Controls.Add(this.buttonUp);
			this.Controls.Add(this.panel2);
			this.Controls.Add(this.buttonOK);
			this.Controls.Add(this.buttonCancel);
			this.Controls.Add(this.buttonRemove);
			this.Controls.Add(this.buttonAdd);
			this.Controls.Add(this.panel1);
			this.Controls.Add(this.toolBar1);
			this.Name = "HtmlDropDownListItemsEditorUI";
			this.Text = "HtmlDropDownList Items Editor";
			this.Activated += new System.EventHandler(this.HtmlDropDownListItemsEditorUI_Activated);
			this.panel1.ResumeLayout(false);
			this.panel2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The angle, measured in degrees clockwise from the x-axis, of the linear gradient's orientation line.
		/// </summary>
		private float Angle
		{
			get { return mAngle; }
			set { mAngle = value; }
		}

		/// <summary>
		/// Gets or sets a Color structure representing the currently selected color.
		/// </summary>
		private Color CurrentColor
		{
			get { return mCurrentColor; }
			set { mCurrentColor = value; }
		}

		/// <summary>
		/// Gets or sets a Color structure representing the currently selected end color of the linear gradient.
		/// </summary>
		private Color EndColor
		{
			get { return mEndColor; }
			set { mEndColor = value; }
		}

		/// <summary>
		/// Gets or sets a the current offset <cref name="BrushType.Shadow"/> brush.
		/// </summary>
		private int Offset
		{
			get { return mOffset; }
			set { mOffset = value; }
		}

		/// <summary>
		/// Gets or sets the index specifying the currently selected item.
		/// </summary>
		private int SelectedIndex
		{
			get { return mSelectedIndex; }
			set { mSelectedIndex = value; }
		}

		/// <summary>
		/// Gets or sets a Color structure representing the currently selected start color of the linear gradient.
		/// </summary>
		private Color StartColor
		{
			get { return mStartColor; }
			set { mStartColor = value; }
		}

		/// <summary>
		/// Gets or sets a Color structure representing the currently selected shadow color of the <cref name="BrushType.Shadow"/> brush.
		/// </summary>
		private Color ShadowColor
		{
			get { return mShadowColor; }
			set { mShadowColor = value; }
		}

		/// <summary>
		/// Gets or sets the Index of the of the ToolBarColor image.
		/// </summary>
		private int ToolBarColorImageIndex
		{
			get { return mToolBarColorImageIndex; }
			set { mToolBarColorImageIndex = value; }
		}

		/// <summary>
		/// Gets or sets the Index of the of the ToolBarLinearGradientBrush image.
		/// </summary>
		private int ToolBarLinearGradientBrushImageIndex
		{
			get { return mToolBarLinearGradientBrushImageIndex; }
			set { mToolBarLinearGradientBrushImageIndex = value; }
		}

		/// <summary>
		/// Gets or sets the Index of the of the ToolBarShadowBrush image.
		/// </summary>
		private int ToolBarShadowBrushImageIndex
		{
			get { return mToolBarShadowBrushImageIndex; }
			set { mToolBarShadowBrushImageIndex = value; }
		}

		/// <summary>
		/// Gets or sets a collection of the items contained in this HtmlDropDownList.
		/// </summary>
		public ComboBox.ObjectCollection Items
		{
			get { return htmlDropDownListEdit.Items; }
		}

		/// <summary>
		/// Handles the ButtonClick event of the toolBarAnchor button.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">A <cref name="ToolBarButtonClickEventArgs"/> that contains the event data.</param>
		private void toolBarAnchor_Click(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			using (AnchorDialog anchorDialog = new AnchorDialog())
			{
				if (htmlDropDownListEdit.SelectionLength == 0)
				{
					anchorDialog.DisplayText = string.Empty;
				}
				else
				{
					anchorDialog.DisplayText = htmlDropDownListEdit.SelectedText;
				}
				if (anchorDialog.ShowDialog() == DialogResult.OK)
				{
					htmlDropDownListEdit.SelectedText = String.Format(CultureInfo.InvariantCulture, "<a id=\"{0}\">{1}</a>", anchorDialog.Id, anchorDialog.DisplayText);
					htmlDropDownListEdit.SelectionStart += anchorDialog.Id.Length + 9;
				}
			}
		}

		/// <summary>
		/// Handles the ButtonClick event of the toolBarBold button.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">A <cref name="ToolBarButtonClickEventArgs"/> that contains the event data.</param>
		private void toolBarBold_Click(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			if (htmlDropDownListEdit.SelectionLength == 0)
			{
				htmlDropDownListEdit.SelectedText = "<B></B>";
			}
			else
			{
				htmlDropDownListEdit.SelectedText = String.Format(CultureInfo.InvariantCulture, "<B>{0}</B>", htmlDropDownListEdit.SelectedText);
			}
			htmlDropDownListEdit.SelectionStart += 3;
		}

		/// <summary>
		/// Handles the ButtonClick event of the toolBarColor button.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">A <cref name="ToolBarButtonClickEventArgs"/> that contains the event data.</param>
		private void toolBarColor_Click(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			using (ColorDialog colorDialog = new ColorDialog())
			{
				colorDialog.Color = CurrentColor;

				if (colorDialog.ShowDialog() == DialogResult.OK)
				{
					CurrentColor =  colorDialog.Color;

					if (htmlDropDownListEdit.SelectionLength == 0)
					{
						htmlDropDownListEdit.SelectedText += String.Format(CultureInfo.InvariantCulture, "<font color=\"#{0:X2}{1:X2}{2:X2}\"/></font>", mCurrentColor.R, mCurrentColor.G, mCurrentColor.B);
					}
					else
					{
						htmlDropDownListEdit.SelectedText = String.Format(CultureInfo.InvariantCulture, "<font color=\"#{0:X2}{1:X2}{2:X2}\"/>{3}</font>", mCurrentColor.R, mCurrentColor.G, mCurrentColor.B, htmlDropDownListEdit.SelectedText);
					}
					htmlDropDownListEdit.SelectionStart += 23;

					imageListToolbar.Images[ToolBarColorImageIndex] = CreateColorBitmapButton(mCurrentColor);
					toolBar1.Invalidate();
				}
			}
		}

		/// <summary>
		/// Handles the ButtonClick event of the toolBarItalic button.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">A <cref name="ToolBarButtonClickEventArgs"/> that contains the event data.</param>
		private void toolBarItalic_Click(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			if (htmlDropDownListEdit.SelectionLength == 0)
			{
				htmlDropDownListEdit.SelectedText = "<I></I>";
			}
			else
			{
				htmlDropDownListEdit.SelectedText = String.Format(CultureInfo.InvariantCulture, "<I>{0}</I>", htmlDropDownListEdit.SelectedText);
			}
			htmlDropDownListEdit.SelectionStart += 3;
		}
		
		/// <summary>
		/// Handles the ButtonClick event of the toolBarUnderline button.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">A <cref name="ToolBarButtonClickEventArgs"/> that contains the event data.</param>
		private void toolBarUnderline_Click(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			if (htmlDropDownListEdit.SelectionLength == 0)
			{
				htmlDropDownListEdit.SelectedText = "<U></U>";
			}
			else
			{
				htmlDropDownListEdit.SelectedText = String.Format(CultureInfo.InvariantCulture, "<U>{0}</U>", htmlDropDownListEdit.SelectedText);
			}
			htmlDropDownListEdit.SelectionStart += 3;
		}

		/// <summary>
		/// Handles the ButtonClick event of the toolBarStrikeThough button.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">A <cref name="ToolBarButtonClickEventArgs"/> that contains the event data.</param>
		private void toolBarStrikeThough_Click(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			if (htmlDropDownListEdit.SelectionLength == 0)
			{
				htmlDropDownListEdit.SelectedText = "<S></S>";
			}
			else
			{
				htmlDropDownListEdit.SelectedText = String.Format(CultureInfo.InvariantCulture, "<S>{0}</S>", htmlDropDownListEdit.SelectedText);
			}
			htmlDropDownListEdit.SelectionStart += 3;
		}

		/// <summary>
		/// Handles the ButtonClick event of the toolBarImage button.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">A <cref name="ToolBarButtonClickEventArgs"/> that contains the event data.</param>
		private void toolBarImage_Click(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			if (htmlDropDownListEdit.ImageList.Images.Count == 0)
			{	
				OpenFileDialog fileDialog = new OpenFileDialog();
				fileDialog.InitialDirectory = htmlDropDownListEdit.BaseHref;
				fileDialog.Filter = "BMP Files (*.BMP)|*.bmp|Icon files (*.ico)|*.ico|All files (*.*)|*.*";
				if (fileDialog.ShowDialog() == DialogResult.OK)
				{
					htmlDropDownListEdit.SelectionLength = 0;
					htmlDropDownListEdit.SelectedText = String.Format(CultureInfo.InvariantCulture, "<img src=\"{0}\"/>", fileDialog.FileName);
				}
			}
		}

		/// <summary>
		/// Handles the ButtonClick event of the toolBarLinearGradientBrush button.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">A <cref name="ToolBarButtonClickEventArgs"/> that contains the event data.</param>
		private void toolBarLinearGradientBrush_Click(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			using (LinearGradientBrushDialog linearGradientBrushDialog = new LinearGradientBrushDialog(StartColor, EndColor, Angle))
			{
				if (linearGradientBrushDialog.ShowDialog() == DialogResult.OK)
				{
					if (htmlDropDownListEdit.SelectionLength == 0)
					{
						htmlDropDownListEdit.SelectedText += String.Format(CultureInfo.InvariantCulture, "{0}{1}", linearGradientBrushDialog.StartTag, linearGradientBrushDialog.EndTag);
					}
					else
					{
						htmlDropDownListEdit.SelectedText = String.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", linearGradientBrushDialog.StartTag, htmlDropDownListEdit.SelectedText, linearGradientBrushDialog.EndTag);
					}
					htmlDropDownListEdit.SelectionStart += linearGradientBrushDialog.StartTag.Length;
					StartColor = linearGradientBrushDialog.StartColor;
					EndColor = linearGradientBrushDialog.EndColor; 
					Angle = linearGradientBrushDialog.Angle;
					imageListToolbar.Images[ToolBarLinearGradientBrushImageIndex] = CreateLinearGradientBrushBitmapButton(linearGradientBrushDialog.StartColor, linearGradientBrushDialog.EndColor, linearGradientBrushDialog.Angle);
					toolBar1.Invalidate();
				}
			}
		}

		/// <summary>
		/// Handles the ButtonClick event of the toolBarShadowBrush button.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">A <cref name="ToolBarButtonClickEventArgs"/> that contains the event data.</param>
		private void toolBarShadowBrush_Click(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			using (ShadowBrushDialog shadowBrushDialog = new ShadowBrushDialog(ShadowColor, Offset))
			{
				if (shadowBrushDialog.ShowDialog() == DialogResult.OK)
				{
					if (htmlDropDownListEdit.SelectionLength == 0)
					{
						htmlDropDownListEdit.SelectedText += String.Format(CultureInfo.InvariantCulture, "{0}{1}", shadowBrushDialog.StartTag, shadowBrushDialog.EndTag);
					}
					else
					{
						htmlDropDownListEdit.SelectedText = String.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", shadowBrushDialog.StartTag, htmlDropDownListEdit.SelectedText, shadowBrushDialog.EndTag);
					}
					htmlDropDownListEdit.SelectionStart += shadowBrushDialog.StartTag.Length;
					ShadowColor = shadowBrushDialog.ShadowColor;
					Offset = shadowBrushDialog.Offset;
					imageListToolbar.Images[ToolBarShadowBrushImageIndex] = CreateShadowBrushBitmapButton(shadowBrushDialog.ShadowColor, shadowBrushDialog.Offset);
					toolBar1.Invalidate();
				}
			}
		}

		/// <summary>
		/// Handles the ToolBar ButtonClick event.  
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">A <cref name="ToolBarButtonClickEventArgs"/> that contains the event data.</param>
		/// <remarks>
		/// Calls the method represented by the <cref name="ToolBarButtonClickEventHandler"/> delegate and stored in the ToolBarButton Tag property.
		/// </remarks>
		private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			((ToolBarButtonClickEventHandler)e.Button.Tag)(sender, e);
			htmlDropDownListEdit.Focus();
		}

		/// <summary>
		/// Handles the SelectedIndexChanged event of the <cref name="comboBoxFonts"/> control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">An <cref name="EventArgs"/> that contains the event data.</param>
		/// <remarks>
		/// Inserts a &lt;font&gt; tag.
		/// </remarks>
		private void comboBoxFonts_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			string lFontTag;
			Font lCurrentFont = new Font(((ComboBox)sender).Text.Split(new Char[] {'"'})[1], 8);

			lFontTag = String.Format(CultureInfo.InvariantCulture, "<font face=\"{0}\">", lCurrentFont.FontFamily.Name);
			if (htmlDropDownListEdit.SelectionLength == 0)
			{
				htmlDropDownListEdit.SelectedText += lFontTag + "</font>";
			}
			else
			{
				htmlDropDownListEdit.SelectedText = lFontTag + htmlDropDownListEdit.SelectedText + "</font>";
			}
			htmlDropDownListEdit.SelectionStart += lFontTag.Length;
			htmlDropDownListEdit.Focus();
		}

		/// <summary>
		/// Handles the Click event of the <cref name="buttonAdd"/> control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">An <cref name="EventArgs"/> that contains the event data.</param>
		/// <remarks>
		/// Adds the <cref name="Text"/> to the <cref name="Items"/> collection.
		/// </remarks>
		private void buttonAdd_Click(object sender, System.EventArgs e)
		{
			htmlDropDownListEdit.Items.Add(htmlDropDownListEdit.Text);	
			htmlDropDownListEdit.Text = string.Empty;
			htmlDropDownListEdit.Focus();
		}

		/// <summary>
		/// Handles the Click event of the <cref name="buttonRemove"/> control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">An <cref name="EventArgs"/> that contains the event data.</param>
		/// <remarks>
		/// Removes the <cref name="SelectedItem"/> to the <cref name="Items"/> collection.
		/// </remarks>
		private void buttonRemove_Click(object sender, System.EventArgs e)
		{
			htmlDropDownListEdit.Items.Remove(htmlDropDownListEdit.SelectedItem);
			htmlDropDownListEdit.Focus();
		}

		/// <summary>
		/// Handles the Click event of the <cref name="buttonCancel"/> control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">An <cref name="EventArgs"/> that contains the event data.</param>
		private void buttonCancel_Click(object sender, System.EventArgs e)
		{
			Close();
		}

		/// <summary>
		/// Handles the Click event of the <cref name="buttonUp"/> control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">An <cref name="EventArgs"/> that contains the event data.</param>
		private void buttonUp_Click(object sender, System.EventArgs e)
		{
			if (htmlDropDownListEdit.SelectedIndex > 0)
			{
				htmlDropDownListEdit.Items.Insert(htmlDropDownListEdit.SelectedIndex - 1, htmlDropDownListEdit.SelectedItem.ToString());
				htmlDropDownListEdit.SelectedIndex = htmlDropDownListEdit.SelectedIndex - 2;
				htmlDropDownListEdit.Items.RemoveAt(htmlDropDownListEdit.SelectedIndex + 2);
			}
		}

		/// <summary>
		/// Handles the Click event of the <cref name="buttonDown"/> control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">An <cref name="EventArgs"/> that contains the event data.</param>
		private void buttonDown_Click(object sender, System.EventArgs e)
		{
			if (htmlDropDownListEdit.SelectedIndex < htmlDropDownListEdit.Items.Count - 1)
			{
				htmlDropDownListEdit.Items.Insert(htmlDropDownListEdit.SelectedIndex + 2, htmlDropDownListEdit.SelectedItem.ToString());
				htmlDropDownListEdit.SelectedIndex = htmlDropDownListEdit.SelectedIndex + 2;
				htmlDropDownListEdit.Items.RemoveAt(htmlDropDownListEdit.SelectedIndex - 2);
			}
		}

		/// <summary>
		/// Handles the Click event of the <cref name="buttonSortAscending"/> control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">An <cref name="EventArgs"/> that contains the event data.</param>
		private void buttonSortAscending_Click(object sender, System.EventArgs e)
		{
			// TODO: Sort on text not html (e.g. "<B>B</B>" should come after "A")
			htmlDropDownListEdit.Sorted = !htmlDropDownListEdit.Sorted;
		}

		/// <summary>
		/// Handles the Click event of the <cref name="buttonOK"/> control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">An <cref name="EventArgs"/> that contains the event data.</param>
		private void buttonOK_Click(object sender, System.EventArgs e)
		{
			if (mSelectedIndex != -1)
			{
				htmlDropDownListEdit.Items[mSelectedIndex] = htmlDropDownListEdit.Text;
			}
		}
	
		/// <summary>
		/// Handles the Click event of the <cref name="buttonApply"/> control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">An <cref name="EventArgs"/> that contains the event data.</param>
		private void buttonApply_Click(object sender, System.EventArgs e)
		{
			buttonOK_Click(sender, e);
		}

		/// <summary>
		/// Handles the <cref name="SelectedIndexChanged"/> event to record the <cref name="SelectedIndex"/>
		/// so we can update it when the OK button is clicked.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">An <cref name="EventArgs"/> that contains the event data.</param>
		private void htmlDropDownListEdit_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			mSelectedIndex = htmlDropDownListEdit.SelectedIndex;
		}

		/// <summary>
		/// Handles the <cref name="Form.Acivated"/> event.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">An <cref name="EventArgs"/> that contains the event data.</param>
		private void HtmlDropDownListItemsEditorUI_Activated(object sender, System.EventArgs e)
		{
			comboBoxFonts.Select(0,0);

			htmlDropDownListEdit.Focus();
		}

		/// <summary>
		/// Handles the <cref name="MenuItem.DrawItem"/> event to draw a custom menu containing images from
		/// the <cref name="Imagelist"/>
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">An <cref name="DrawItemEventArgs"/> that contains the event data.</param>
		private void menuItemImage_DrawItem(object sender, DrawItemEventArgs e)
		{
			e.DrawBackground();
			e.Graphics.DrawImage(htmlDropDownListEdit.ImageList.Images[((MenuItem)sender).Index], e.Bounds); //, e.Bounds.X + 1, e.Bounds.Y + 1);
		}

		/// <summary>
		/// Handles the <cref name="MenuItem.MeasureItem"/> event to set the drawing area of a custom menu containing images from
		/// the <cref name="Imagelist"/>
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">A <cref name="MeasureItemEventArgs"/> that contains the event data.</param>
		private void menuItemImage_MeasureItem(object sender, MeasureItemEventArgs e)
		{
			e.ItemHeight = 18;
			e.ItemWidth = 6; // Should be 18 but we will use up the 12 pixels provided for the checkbox.
		}

		/// <summary>
		/// Handles the <cref name="MenuItem.Click"/> event to insert a &lt;img&gt; tag.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">An <cref name="EventArgs"/> that contains the event data.</param>
		private void menuItemImage_Click(object sender, System.EventArgs e)
		{
			htmlDropDownListEdit.SelectionLength = 0;
			htmlDropDownListEdit.SelectedText = string.Format(CultureInfo.InvariantCulture, "<img src=\"#{0}\"/>", ((MenuItem)sender).Index);	
		}

		/// <summary>
		/// Creates a 16 by 16 pixel bitmap containing the letter "A" in the Arial font using a Shadow brush 
		/// with the ShadowColor specified by <paramref name="color"/> and Offset specified by <paramref name="offset"/>.
		/// </summary>
		/// <param name="color">The ShadowColor specified.</param>
		/// <param name="offset">The shadow offset specified.</param>
		/// <returns>A 16 by 16 pixel bitmap.</returns>
		private static Bitmap CreateShadowBrushBitmapButton(Color color, float offset)
		{
			Bitmap lBitmap = new Bitmap(16, 16);
			Graphics lGraphics = Graphics.FromImage(lBitmap);
			Font lFont = new Font("Arial", 10, FontStyle.Bold);
			SizeF lSizeF = lGraphics.MeasureString("A", lFont);
			float lScale = Math.Min(lBitmap.Width / lSizeF.Width, lBitmap.Height / lSizeF.Height);
   
			lFont = new Font(lFont.Name, lScale * lFont.SizeInPoints, lFont.Style);
   
			lGraphics.Clear(Color.White);
			lGraphics.DrawString("A", lFont, new SolidBrush(color), 0, 0);
			lGraphics.DrawString("A", lFont, new SolidBrush(SystemColors.WindowText), offset, offset);
			lGraphics.Dispose();
   
			if (color != Color.White)
			{
				lBitmap.MakeTransparent(Color.White);
			}
			return lBitmap;
		}
			
		/// <summary>
		/// Creates a 16 by 16 pixel bitmap using a <cref name="LinearGradientBrush"/> with a starting color specified by <paramref name="color1"/>,
		/// an ending color specified by <paramref name="color2"/> and an angle specified by <paramref name="angle"/>.
		/// </summary>
		/// <param name="color1">The starting color of the LinearGradient brush.</param>
		/// <param name="color2">The ending color of the LinearGradient brush.</param>
		/// <param name="angle">The angle of the LinearGradient brush.</param>
		/// <returns>A 16 by 16 pixel bitmap.</returns>
		private static Bitmap CreateLinearGradientBrushBitmapButton(Color color1, Color color2, float angle)
		{
			Bitmap lBitmap = new Bitmap(16, 16);
			Graphics lGraphics = Graphics.FromImage(lBitmap);

			Brush lBrush = new LinearGradientBrush(new Rectangle(0, 0, 16, 16), color1, color2, angle);

			lGraphics.FillRectangle(lBrush, 0, 0, 16, 16);
			lGraphics.Dispose();

			return lBitmap;
		}
			
		/// <summary>
		/// Creates a 16 by 16 pixel bitmap containing the letter "A" in the Arial font and of the color specified by <paramref name="color"/>.
		/// </summary>
		/// <param name="color">The color used in drawing the "A" character.</param>
		/// <returns>A 16 by 16 pixel bitmap.</returns>
		private static Bitmap CreateColorBitmapButton(Color color)
		{
			Bitmap lBitmap = new Bitmap(16, 16);
			Graphics lGraphics = Graphics.FromImage(lBitmap);
			Font lFont = new Font("Arial", 10, FontStyle.Bold);
			SizeF lSizeF = lGraphics.MeasureString("A", lFont);
			float lScale = Math.Min(lBitmap.Width / lSizeF.Width, lBitmap.Height / lSizeF.Height);
   
			lFont = new Font(lFont.Name, lScale * lFont.SizeInPoints, lFont.Style);
			StringFormat lStringFormat = new StringFormat();
			lStringFormat.Alignment = lStringFormat.LineAlignment = StringAlignment.Center;
   
			lGraphics.Clear(Color.White);
			lGraphics.DrawString("A", lFont, new SolidBrush(color), lBitmap.Width / 2, lBitmap.Height / 2, lStringFormat);
			lGraphics.Dispose();
   
			if (color != Color.White)
			{
				lBitmap.MakeTransparent(Color.White);
			}
			return lBitmap;
		}
	}
}